package com.xyh.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xyh.dao.UserMapper;
import com.xyh.po.User;
import com.xyh.service.impl.UserServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @Author:kyle
 * @Date:2020/12/16 - 9:34
 * @Project:spring_demo1
 * @Package:com.xyh.controller
 * @Version:1.0
 */
@Controller
@RequestMapping("/user")//加上前缀/user
public class HelloController {

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private UserServiceImpl userService;

    //"/hello"拦截页面地址，相当于之前Servlet配置的urlPatterns
    @RequestMapping("/hello")//RequestMapping括号里的/可以不写，会自动加上
    public String hello(){//String是路径地址
        System.out.println("HelloController");

        return "hello";//springmvc跳转默认使用的是请求转发
    }

    //  restful风格 {}
    @RequestMapping("/hello2/{name}")
    public String list2(HttpServletRequest request,@PathVariable("name") String name){
        System.out.println("helloRequest====" + name);
        List<User> list = userService.list();
        list.stream().forEach(System.out::println);
        request.setAttribute("request",list);
        return "hello";
    }

    @RequestMapping("/h/{id}")
    public String hello1(HttpServletRequest request, @PathVariable("id") Integer id){
        System.out.println("id = " + id);
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.eq("role_pkid",id);
        List<User> users = userMapper.selectByLinkWrapperPage(wrapper);
        request.setAttribute("request",users);
        return "hello";
    }
}
